package com.achievo.know.web.category;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Hibernate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.achievo.know.application.CategoryApplication;
import com.achievo.know.domain.Category;
import com.achievo.know.domain.CategoryContent;
import com.achievo.know.web.util.BaseActionSupport;
import com.redhat.redwork.coc.RedworkAction;

public class CategoryAction extends BaseActionSupport implements RedworkAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 146318772967407141L;
	
	Logger logger=LoggerFactory.getLogger(CategoryAction.class);
	
	@Autowired
	private CategoryApplication categoryApplication;
	
	public void setCategoryApplication(CategoryApplication categoryApplication) {
		this.categoryApplication = categoryApplication;
	}

	private String data;
	
	private Category cate;
	
	private String jsonResult;
	
	private String code;
	
	private String parent;
	
	private String ids;
	
	private String id;
	
	private String toId;
	
	private String type;
	
	private CategoryContent content;
	
	public String show(){
		if(isSuperAdmin())
			data=categoryApplication.getJson(null);
		else
			data=categoryApplication.getJson(getAdminCateIds());
		return "show";
	}
	
	public String home(){
		if(isSuperAdmin())
			data=categoryApplication.getJson(null);
		else
			data=categoryApplication.getJson(getAdminCateIds());
		return "home";
	}
	
	public String showAdd(){
		if(isSuperAdmin())
			data=categoryApplication.getJsonWithRoot(null);
		else
			data=categoryApplication.getJsonWithRoot(getAdminCateIds());
		return "add";
	}
	
	public String showEdit(){
		if(isSuperAdmin())
			data=categoryApplication.getJsonWithRoot(null);
		else
			data=categoryApplication.getJsonWithRoot(getAdminCateIds());
		cate=categoryApplication.get(Long.parseLong(id));
		return "add";
	}
	
	public String showDesc(){
		content=categoryApplication.getDesc(Long.parseLong(id));
		return "desc";
	}
	
	public String desc(){
		try{
			if(content!=null){
				if(content.getId()!=null){
					CategoryContent temp=categoryApplication.getDesc(Long.parseLong(id));
					temp.setContent(Hibernate.createBlob(content.getContStr().getBytes()));
					categoryApplication.saveDesc(temp);
				}else{
					content.setCateId(Long.parseLong(id));
					content.setContent(Hibernate.createBlob(content.getContStr().getBytes()));
					categoryApplication.saveDesc(content);
				}
			}
			jsonResult="保存成功";
		}catch (Exception e) {
			logger.error(e.getMessage());
			jsonResult="保存出错："+e.getMessage();
		}
		
		return home();
	}
	
	public String add(){
		try{
			if(StringUtils.isEmpty(parent)){
				
			}else{
				Category p=categoryApplication.get(Long.parseLong(parent));
				cate.setParent(p);
			}
			if("on".equals(cate.getShowStr())){
				cate.setShowInNav(true);
			}else{
				cate.setShowInNav(false);
			}
			categoryApplication.add(cate);
			code="1";
			jsonResult="保存成功";
		}catch (Exception e) {
			logger.error(e.getMessage());
			code="0";
			jsonResult="保存出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String edit(){
		try{
			Category cTemp=categoryApplication.get(Long.parseLong(id));
			if(StringUtils.isEmpty(parent)){
				
			}else{
				Category p=categoryApplication.get(Long.parseLong(parent));
				cTemp.setParent(p);
			}
			if("on".equals(cate.getShowStr())){
				cTemp.setShowInNav(true);
			}else{
				cTemp.setShowInNav(false);
			}
			cTemp.setName(cate.getName());
			categoryApplication.save(cTemp);
			code="1";
			jsonResult="保存成功";
		}catch (Exception e) {
			logger.error(e.getMessage());
			code="0";
			jsonResult="保存出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String delete(){
		try{
			if(StringUtils.isNotEmpty(ids)){
				String[] idA=ids.split(",");
				categoryApplication.deleteBatch(idA);
			}
			code="1";
			jsonResult="保存成功";
		}catch (Exception e) {
			logger.error(e.getMessage());
			code="0";
			jsonResult="保存出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String changePos(){
		try{
			if(StringUtils.isNotEmpty(id)&& StringUtils.isNotEmpty(toId)){
				categoryApplication.changePos(id,toId,type);
			}
			code="1";
			jsonResult="保存成功";
		}catch (Exception e) {
			logger.error(e.getMessage());
			code="0";
			jsonResult="保存出错："+e.getMessage();
		}
		return JSON;
	}
	
	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public Category getCate() {
		return cate;
	}

	public void setCate(Category cate) {
		this.cate = cate;
	}

	public String getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getParent() {
		return parent;
	}

	public void setParent(String parent) {
		this.parent = parent;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getToId() {
		return toId;
	}

	public void setToId(String toId) {
		this.toId = toId;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public CategoryContent getContent() {
		return content;
	}

	public void setContent(CategoryContent content) {
		this.content = content;
	}
	
}
